package io.renren.modules.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.validator.Assert;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.form.LoginForm;
import io.renren.modules.app.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Override
	public UserEntity queryByMobile(String mobile) {
		UserEntity userEntity = new UserEntity();
		userEntity.setMobile(mobile);
		return baseMapper.selectOne(userEntity);
	}

	@Override
	public long login(LoginForm form) {
		UserEntity user = queryByMobile(form.getMobile());
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			throw new RRException("手机号或密码错误");
		}

		return user.getUserId();
	}

	@Override
	@Async
	public boolean addUser() {
		System.out.println("addUser 执行");

		UserEntity userEntity = selectById(2);

		try {
			Thread.sleep(3000 * 1);
			System.out.println("休眠之后");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		userEntity.setMobile("13677160919");
		userEntity.setCreateTime(new Date());
		userEntity.setPassword("123456");
		userEntity.setUsername(userEntity.getUsername() + "addUser1更新");
		System.out.println("被更新：" + userEntity.getUsername());
		return updateById(userEntity);

	}

	@Override
	@Async
	public boolean addUser2() {
		System.out.println("addUser2 执行");
		UserEntity userEntity = selectById(2);
		userEntity.setUsername(userEntity.getUsername()+"addUser2更新");
		System.out.println("被更新：" + userEntity.getUsername());

		return 	updateById(userEntity);
	}


}
